package com.softlab.retailwholesalesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softlab.retailwholesalesystem.entity.*;
import com.softlab.retailwholesalesystem.entity.VO.StatisticsGoodsVOEntity;
import com.softlab.retailwholesalesystem.service.IStatisticsService;
import com.softlab.retailwholesalesystem.service.exception.UpdateErrorException;
import com.softlab.retailwholesalesystem.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/statistics")
public class StatisticsController {
    @Autowired
    private IStatisticsService service;

    @GetMapping
    public JsonResult<List<StatisticsGoodsVOEntity>> getAll() {
        QueryWrapper<StatisticsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
        List<StatisticsGoodsVOEntity> list = service.getAll(wrapper);
        return new JsonResult<>(200, "盘点列表查询成功", list);
    }

    @RequestMapping("/init")
    @GetMapping
    public JsonResult<Void> init(){
        service.init();
        return new JsonResult<>(200, "盘点成功");
    }

    @RequestMapping("/all")
    @GetMapping
    public JsonResult<List<Double>> getStatusStatistics(){
        List<Double> statusStatistics = service.getStatusStatistics();
        return new JsonResult<>(200, "盘点成功", statusStatistics);
    }


    @PutMapping
    public JsonResult<Void> addPic(@RequestBody StatisticsEntity entity) {
        boolean b = service.updateById(entity);
        if (!b) throw new UpdateErrorException("修改时遇到错误");
        return new JsonResult<>(200, "图片增加成功");
    }


    @GetMapping("/search")
    public JsonResult<List<StatisticsGoodsVOEntity>> search(StatisticsEntity entity) {
        List<StatisticsGoodsVOEntity> list = service.search(entity);
        return new JsonResult<>(200, "盘点列表查找成功", list);
    }

    @PostMapping("/worker")
    public JsonResult<List<WorkerStatistics>> getWorkerStatistics(@RequestBody Average average) {
        List<WorkerStatistics> workerStatistics = service.getWorkerStatistics(average);
        return new JsonResult<>(200, "销售员盘点成功", workerStatistics);
    }

    @PostMapping("/specworker")
    public JsonResult<List<WorkerStatistics>> getSpecWorker(@RequestBody SpecificWorker worker) {


        List<WorkerStatistics> workerStatistics = service.getSomeWorker(worker);
        if (workerStatistics == null){
            return new JsonResult<>(404, "查找失败", null);
        }
        return new JsonResult<>(200, "销售员盘点成功", workerStatistics);
    }


    @GetMapping("/goods")
    public JsonResult<List<GoodsStatistics>> getGoodsStatistics() {
        List<GoodsStatistics> goodsStatistics = service.getGoodsStatistics();
        return new JsonResult<>(200, "货品" +
                "盘点成功", goodsStatistics);
    }

    @PostMapping("/specgoods")
    public JsonResult<List<GoodsStatistics>> getSpecGoods(@RequestBody FlexEntity flexEntity) {
        List<GoodsStatistics> goodsStatisticsList = service.getSomeGoods(flexEntity.getGoodsName());

        if (goodsStatisticsList == null){
            return new JsonResult<>(404, "查找失败", null);
        }
        return new JsonResult<>(200, "货品盘点成功", goodsStatisticsList);
    }
}
